package br.uff.javabasico.DAO;

import br.uff.javabasico.exception.BDException;
import br.uff.javabasico.modelos.Cliente;
import br.uff.javabasico.util.Conexao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClienteDAOImpl {
    
    private ClienteDAOImpl() {
    }
    
    public static Cliente recuperaUmCliente(int id) throws BDException{
        Cliente p = null;
        PreparedStatement pstmt;
        try {
            pstmt = Conexao.getConnection().prepareStatement("select * from cliente where cliente.id=?");
            
            pstmt.setInt(1,id);
            ResultSet rs = pstmt.executeQuery();
            
            if(rs.next()) {
                p = new Cliente();
                p.setNome(rs.getString("nome"));
                p.setNumero(id);                
                p.setLogin(rs.getString("login"));
                p.setSenha(rs.getString("senha"));
            }
            
        } catch (SQLException ex) {
            throw new BDException(ex);
        }finally {
            Conexao.close();
        }
        return p;
    }
    
    public static List<Cliente> getLista() throws BDException{
        PreparedStatement pstmt;
        Cliente cli;
        ArrayList<Cliente> lista = new ArrayList<Cliente>();
        
        try {
            pstmt = Conexao.getConnection().prepareStatement("select * from cliente order by id");
            
            ResultSet rs = pstmt.executeQuery();
            
            while(rs.next()) {
                cli = new Cliente();
                cli.setNome(rs.getString("nome"));
                cli.setNumero(rs.getInt("id"));
                cli.setLogin(rs.getString("login"));
                cli.setSenha(rs.getString("senha"));
                lista.add(cli);
            }
        } catch (SQLException ex) {
            throw new BDException(ex);
        }finally {
            Conexao.close();
        }
        return lista;
    }
    
    public static Cliente recuperaUmCliente(String login, String senha) throws BDException{
        Cliente cli = recuperaUmCliente(login);
        if (cli != null && !cli.getSenha().equals(senha) ) cli = null;
        return cli;
    }
    
    public static Cliente recuperaUmCliente(String login) throws BDException{
        Cliente p = null;
        PreparedStatement pstmt;
        try {
            pstmt = Conexao.getConnection().prepareStatement("select * from cliente where cliente.login=?");
            
            pstmt.setString(1,login);
            ResultSet rs = pstmt.executeQuery();
            
            if(rs.next()) {
                p = new Cliente();
                p.setNome(rs.getString("nome"));
                p.setNumero(rs.getInt("id"));
                p.setLogin(rs.getString("login"));
                p.setSenha(rs.getString("senha"));
            }
            
        } catch (SQLException ex) {
            throw new BDException(ex);
        }finally {
            Conexao.close();
        }
        return p;
    }
    
//    public static Cliente recuperaUmClienteEItensPedidos(int id) throws BDException{
//        Cliente p = null;
//        PreparedStatement pstmt;
//        try {
//            pstmt = Conexao.getConnection().prepareStatement("select * from cliente left outer join item   on id_cliente=? where cliente.id=?");
//            
//            pstmt.setInt(1,id);
//            pstmt.setInt(2,id);
//            ResultSet rs = pstmt.executeQuery();
//            if(rs.next()) {
//                List<String> listaItems = new ArrayList<String>();
//                p = new Cliente();
//                p.setNome(rs.getString("nome"));
//                p.setNumero(id);
//                int num_produto = rs.getInt("num_produto");
//                if(num_produto!=0)
//                    listaItems.add(""+num_produto);
//                while(rs.next()){
//                    listaItems.add(""+rs.getInt("num_produto"));
//                }
//                p.setListaDeItens(listaItems);
//            }
//            
//        } catch (SQLException ex) {
//            throw new BDException(ex);
//        }finally {
//            Conexao.close();
//        }
//        return p;
//    }
    
    public static void inclui(Cliente cli) throws BDException{
        PreparedStatement pstmt;
        try {
            pstmt = Conexao.getConnection().prepareStatement("insert into cliente (nome, login, senha) values (?,?,?)");
            
            pstmt.setString(1,cli.getNome());
            pstmt.setString(2,cli.getLogin());
            pstmt.setString(3,cli.getSenha());
            
            pstmt.execute();
            
        } catch (SQLException ex) {
            throw new BDException(ex);
        }finally {
            Conexao.close();
        }
    }
    
    public static void altera(Cliente cli) throws BDException{
        PreparedStatement pstmt;
        try {
            pstmt = Conexao.getConnection().prepareStatement("Update cliente set nome =? ,senha=? where id = ?");
            
            pstmt.setString(1, cli.getNome());
            pstmt.setString(2, cli.getSenha());
            pstmt.setInt(3, cli.getNumero());
            int retorno = pstmt.executeUpdate();
            if ( retorno == 0 ) throw new BDException("Nenhum cliente foi atualizado");
        } catch (SQLException ex) {
            throw new BDException(ex);
        }
        
        
    }
    
    public static void exclui(int id) throws BDException{
        PreparedStatement pstmt;
        try {
            pstmt = Conexao.getConnection().prepareStatement("delete from cliente where id=?");
            
            pstmt.setInt(1,id);
            ResultSet rs = pstmt.executeQuery();
        } catch (SQLException ex) {
            throw new BDException(ex);
        }finally {
            Conexao.close();
        }
    }
    
}
